<?php

namespace App\Http\Controllers\Lottery;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class LotteryController extends Controller
{
    /*
     * 连号遗漏
     */
    public function evenMissing(Request $request){
        $type = $request->get('type');
        $result = array();
        switch($type){
            case 1:
                //黑龙江11选5
                $data =$this->exhaustive('11', 3);
                $result['三连号（冷号）'] = (array)DB::table('黑龙江11选5_任选三')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['三连号（回补）'] = (array)DB::table('黑龙江11选5_任选三')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 4);
                $result['四连号（冷号）'] = (array)DB::table('黑龙江11选5_任选四')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['四连号（回补）'] = (array)DB::table('黑龙江11选5_任选四')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 5);
                $result['五连号（冷号）'] = (array)DB::table('黑龙江11选5_任选五')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['五连号（回补）'] = (array)DB::table('黑龙江11选5_任选五')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 3);
                $result['前组连号（冷号）'] = (array)DB::table('黑龙江11选5_前组')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['前组连号（回补）'] = (array)DB::table('黑龙江11选5_前组')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                break;
            case 2:
                //黑龙江快乐十分
                $data = $this->exhaustive('11', 3);
                $result['三连号（冷号）'] = (array)DB::table('黑龙江快乐十分_任选三')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['三连号（回补）'] = (array)DB::table('黑龙江快乐十分_任选三')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 4);
                $result['四连号（冷号）'] = (array)DB::table('黑龙江快乐十分_任选四')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['四连号（回补）'] = (array)DB::table('黑龙江快乐十分_任选四')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 5);
                $result['五连号（冷号）'] = (array)DB::table('黑龙江快乐十分_任选五')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['五连号（回补）'] = (array)DB::table('黑龙江快乐十分_任选五')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 3);
                $result['前组连号（冷号）'] = (array)DB::table('黑龙江快乐十分_前组')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['前组连号（回补）'] = (array)DB::table('黑龙江快乐十分_前组')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                break;
            case 6:
                //四川快乐12
                $data = $this->exhaustive('11', 3);
                $result['三连号（冷号）'] = (array)DB::table('四川快乐12_任选三')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['三连号（回补）'] = (array)DB::table('四川快乐12_任选三')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data = $this->exhaustive('11', 4);
                $result['四连号（冷号）'] = (array)DB::table('四川快乐12_任选四')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['四连号（回补）'] = (array)DB::table('四川快乐12_任选四')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data =$this-> exhaustive('11', 5);
                $result['五连号（冷号）'] = (array)DB::table('四川快乐12_任选五')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['五连号（回补）'] = (array)DB::table('四川快乐12_任选五')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                $data =$this-> exhaustive('11', 3);
                $result['前组连号（冷号）'] = (array)DB::table('四川快乐12_前组')->whereIn('号码', $data)->orderby('当前遗漏', 'desc')->first();
                $result['前组连号（回补）'] = (array)DB::table('四川快乐12_前组')->whereIn('号码', $data)->orderby('上次遗漏', 'desc')->first();
                break;
            default:
                return response()->json(['msg' => '0', 'data' => '参数错误']);
        }
        return response()->json(['msg' => '1', 'data' => $result]);
    }


    //连号穷举
    public function exhaustive($max, $size){
        $result = array();
        for($i=1;$i < $max-$size+2;$i++){
            if($i<10){
                $b = str_pad($i,2,'0',STR_PAD_LEFT);
            }else{
                $b = $i;
            }
            for($j=1;$j<$size;$j++){
                $c = $i+$j;
                if($c<10){
                    $d = str_pad($c,2,'0',STR_PAD_LEFT);
                }else{
                    $d = $c;
                }
                $b .= ' '.$d;
            }
            array_push($result,$b);
        }
        return $result;
    }

    /*
     * 获取遗漏统计
     */
    public function getMissing(Request $request){
        $type = $request->input('type');

        switch ($type){
            case '1':
                    $table = '黑龙江11选5';
                break;
            case '2':
                    $table = '黑龙江快乐十分';
                 break;
            case '3':
                    $table = '吉林快3';
                break;
            case '4':
                    $table = '吉林时时彩';
                break;
            case '5':
                    $table = '四川金7乐';
                break;
            case '6':
                    $table = '四川快乐12';
                break;
            default:
                return response()->json(['msg'=>0,'data' => '没有该游戏']);
        }

        if($type == 3){

            $notThreeSame = $this->notSame(6,3);
            $notTwoSame =$this->notSame(6,2);
            $notThreeCold = DB::table($table.'_遗漏')->whereIn("号码", $notThreeSame)->orderby('当前遗漏','desc')->limit(2)->get();
            $notThree = DB::table($table.'_遗漏')->whereIn("号码", $notThreeSame)->orderby('上次遗漏','desc 7')->limit(2)->get();

            $notTwoCold = DB::table($table.'_遗漏')->whereIn("号码",$notTwoSame)->orderby('当前遗漏','desc')->limit(2)->get();
            $notTwo= DB::table($table.'_遗漏')->whereIn("号码",$notTwoSame)->orderby('上次遗漏','desc')->limit(2)->get();
            $result = [$notThreeCold[0],$notThreeCold[1],$notThree[0],$notThree[1],$notTwoCold[0],$notTwoCold[1],$notTwo[0],$notTwo[1]];
            return response()->json(['msg'=>1,'data' => $result]);

        }else if($type == 5){

            $notThreeSame = $this->notSame(7,3);
            $notTwoSame =$this->notSame(7,2);
            $notThreeCold = DB::table($table.'_遗漏')->whereIn("号码", $notThreeSame)->orderby('当前遗漏','desc')->limit(2)->get();
            $notThree = DB::table($table.'_遗漏')->whereIn("号码", $notThreeSame)->orderby('上次遗漏','desc')->limit(2)->get();
            $notTwoCold = DB::table($table.'_遗漏')->whereIn("号码",$notTwoSame)->orderby('当前遗漏','desc')->limit(2)->get();
            $notTwo= DB::table($table.'_遗漏')->whereIn("号码",$notTwoSame)->orderby('上次遗漏','desc')->limit(2)->get();
            $result = [$notThreeCold[0],$notThreeCold[1],$notThree[0],$notThree[0],$notThree[1],$notTwoCold[0],$notTwoCold[1],$notTwo[0],$notTwo[1]];
            return response()->json(['msg'=>1,'data' => $result]);
        }
        else{

            $anyTwoClod = DB::table($table.'_任选二')->orderby('当前遗漏','desc')->limit(1)->get();
            $anyTwo = DB::table($table.'_任选二')->orderby('上次遗漏','desc')->limit(1)->get();
            $anyThreeClod = DB::table($table.'_任选三')->orderby('当前遗漏','desc')->limit(1)->get();
            $anyThree = DB::table($table.'_任选三')->orderby('上次遗漏','desc')->limit(1)->get();
            $anyFourClod = DB::table($table.'_任选四')->orderby('当前遗漏','desc')->limit(1)->get();
            $anyFour = DB::table($table.'_任选四')->orderby('上次遗漏','desc')->limit(1)->get();
            $anyFiveClod = DB::table($table.'_任选五')->orderby('当前遗漏','desc')->limit(1)->get();
            $anyFive = DB::table($table.'_任选五')->orderby('上次遗漏','desc')->limit(1)->get();
            $result= [$anyTwoClod[0],$anyTwo[0],$anyThreeClod[0],$anyThree[0],$anyFourClod[0],$anyFour[0],$anyFiveClod[0],$anyFive[0]];
            return response()->json(['msg'=>1,'data' => $result]);
        }


    }

    /*
     * 获取单双遗漏
     */

    public function getSingleAndDoubleMissing(Request $request){
        $type = $request->input('type');
        switch ($type){
            case '1':
                $table = '黑龙江11选5';
                break;
            case '2':
                $table = '黑龙江快乐十分';
                break;
            case '4':
                $table = '吉林时时彩';
                break;
            case '6':
                $table = '四川快乐12';
                break;
            default:
                return response()->json(['msg'=>0,'data' => '没有该游戏']);
        }

        $allSingleThree = $this->getSingleMissingNum(11,3);
        $allDoubleThree = $this->getDoubleMissingNum(11,3);

        $allSingleThreeCold = DB::table($table.'_任选三')->whereIn('号码',$allSingleThree)->orderby('当前遗漏','desc')->limit(1)->get();
        $allSingleThreeBefore = DB::table($table.'_任选三')->whereIn('号码',$allDoubleThree)->orderby('上次遗漏','desc')->limit(1)->get();

        $allSingleGroupCold = DB::table($table.'_前组')->whereIn('号码',$allSingleThree)->orderby('当前遗漏','desc')->limit(1)->get();
        $allSingleGroupBefore = DB::table($table.'_前组')->whereIn('号码',$allDoubleThree)->orderby('上次遗漏','desc')->limit(1)->get();

        $allSingleFour = $this->getSingleMissingNum(11,4);
        $allDoubleFour = $this->getDoubleMissingNum(11,4);
        $allSingleFourCold = DB::table($table.'_任选四')->whereIn('号码',$allSingleFour)->orderby('当前遗漏','desc')->limit(1)->get();
        $allSingleFourBefore = DB::table($table.'_任选四')->whereIn('号码',$allDoubleFour)->orderby('上次遗漏','desc')->limit(1)->get();

        $allSingleFive = $this->getSingleMissingNum(11,5);
        $allDoubleFive = $this->getDoubleMissingNum(11,5);
        $allSingleFiveCold = DB::table($table.'_任选五')->whereIn('号码',$allSingleFive)->orderby('当前遗漏','desc')->limit(1)->get();
        $allSingleFiveBefore = DB::table($table.'_任选五')->whereIn('号码',$allDoubleFive)->orderby('上次遗漏','desc')->limit(1)->get();

        $result = [
            $allSingleThreeCold[0],
            $allSingleThreeBefore[0],
            $allSingleFourCold[0],
            $allSingleFourBefore[0],
            $allSingleFiveCold[0],
            $allSingleFiveBefore[0],
            $allSingleGroupCold[0],
            $allSingleGroupBefore[0]
        ];
        return response()->json(['msg'=>1,'data' => $result]);

    }
    /*
     * 快3和金7乐的三(二)不同的所有号码
     */
    public function notSame($max,$num){
        $result=[];
        if($num === 3 ){
            for($i =1;$i<$max-1;$i++){
                for($j = $i+1;$j<$max;$j++){
                    for($s = $j+1;$s<$max+1;$s++){
                        array_push($result,$i.$j.$s);
                    }
                }
            }
            return $result;
        }else{
            for($i=1;$i<$max;$i++){
                for($j=$i+1;$j<$max+1;$j++){
                    array_push($result,$i.$i.$j);
                    array_push($result,$i.$j.$j);
                }
            }
            return $result;
        }
    }
    /*
     * 全单遗漏号码
     */
    public function getSingleMissingNum($max,$num){
        $result=[];
        $array  = [];
        for($i =1;$i<$max+1;$i++){
            if($i%2==1){
                if($i<10){
                    $i = '0'.$i;
                }else{
                    $i = $i;
                }
                array_push($array,$i);
            }
        }

        if($num === 3){
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        array_push($result,$array[$j].' '.$array[$s].' '.$array[$a]);
                    }
                }
            }
        }else if($num === 4){
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        for($b = $a+1;$b<count($array);$b++){
                            array_push($result,$array[$j].' '.$array[$s].' '.$array[$a].' '.$array[$b]);
                        }
                    }
                }
            }
        }else{
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        for($b = $a+1;$b<count($array);$b++){
                            for($c = $b+1;$c < count($array);$c++){
                                array_push($result,$array[$j].' '.$array[$s].' '.$array[$a].' '.$array[$b].' '.$array[$c]);
                            }
                        }
                    }
                }
            }
        }

       return $result;
    }
    /*
     * 全双遗漏号码
     */
    public function getDoubleMissingNum($max,$num){
        $result=[];
        $array  = [];
        for($i =1;$i<$max+1;$i++){
            if($i%2==0){
                if($i<10){
                    $i = '0'.$i;
                }else{
                    $i = $i;
                }
                array_push($array,$i);
            }
        }

        if($num === 3){
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        array_push($result,$array[$j].' '.$array[$s].' '.$array[$a]);
                    }
                }
            }
        }else if($num === 4){
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        for($b = $a+1;$b<count($array);$b++){
                            array_push($result,$array[$j].' '.$array[$s].' '.$array[$a].' '.$array[$b]);
                        }
                    }
                }
            }
        }else{
            for($j = 0;$j<count($array);$j++){
                for($s = $j+1;$s<count($array);$s++){
                    for($a = $s+1;$a<count($array);$a++){
                        for($b = $a+1;$b<count($array);$b++){
                            for($c = $b+1;$c < count($array);$c++){
                                array_push($result,$array[$j].' '.$array[$s].' '.$array[$a].' '.$array[$b].' '.$array[$c]);
                            }
                        }
                    }
                }
            }
        }

        return $result;
    }

    /*
     * 最冷排名
     */

    public function getCold(Request $request){
        $type = $request->get('type');

        switch ($type){
            case '1':
                $table = '黑龙江11选5';
                break;
            case '2':
                $table = '黑龙江快乐十分';
                break;
            case '6':
                $table = '四川快乐12';
                break;
            default:
                return response()->json(['msg'=>0,'data' => '没有该游戏']);
        }

        if($type != 2){
            $result['前组'] = DB::table($table.'_前组')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['前直']  = DB::table($table.'_前直')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选二']  = DB::table($table.'_任选二')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选三']  = DB::table($table.'_任选三')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选四']  = DB::table($table.'_任选四')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选五']  = DB::table($table.'_任选五')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
        }else{
            $result['前组'] = DB::table($table.'_前组')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选二']  = DB::table($table.'_任选二')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选三']  = DB::table($table.'_任选三')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选四']  = DB::table($table.'_任选四')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
            $result['任选五']  = DB::table($table.'_任选五')->select('号码','当前遗漏')->orderby('当前遗漏','desc')->limit(5)->get();
        }

        return response()->json(['msg'=>1,'data' => $result]);

    }

    /*
     * 获取推荐信息
     */
    public function getRecommend(Request $request){
        $type = $request->get('type');
        switch ($type){
            case '1':
                $table = '黑龙江11选5';
                break;
            case '2':
                $table = '黑龙江快乐十分';
                break;
            case '6':
                $table = '四川快乐12';
                break;
            default:
                return response()->json(['msg'=>0,'data' => '没有该游戏']);
        }

        $result = DB::table('admin_game')->where('游戏中文名',$table)->select('号码推荐')->first();
        preg_match_all("/\[.*?\//",$result->号码推荐,$a);
        $a[0][0]=trim($a[0][0],'[/');
        $a[0][1]=trim($a[0][1],'[/');
        $a[0][2]=trim($a[0][2],'[/');
        $a[0][3]=trim($a[0][3],'[/');
        $a[0][4]=trim($a[0][4],'[/');
        preg_match_all("/推出.*?期/",$result->号码推荐,$b);
        preg_match_all("/上次推荐.*?\//",$result->号码推荐,$c);
        $c[0][0]=trim($c[0][0],'上次推荐/');
        $c[0][1]=trim($c[0][1],'上次推荐/');
        $c[0][2]=trim($c[0][2],'上次推荐/');
        $c[0][3]=trim($c[0][3],'上次推荐/');
        $c[0][4]=trim($c[0][4],'上次推荐/');
        preg_match_all("/\/[0-9]*?期中出/",$result->号码推荐,$d);
        $d[0][0]=ltrim($d[0][0],'/');
        $d[0][1]=ltrim($d[0][1],'/');
        $d[0][2]=ltrim($d[0][2],'/');
        $d[0][3]=ltrim($d[0][3],'/');
        $d[0][4]=ltrim($d[0][4],'/');
        $re[0] = ['二码推荐',$a[0][0],$b[0][0],'上次推荐',$c[0][0],$d[0][0]];
        $re[1] = ['三码推荐',$a[0][1],$b[0][1],'上次推荐',$c[0][1],$d[0][1]];
        $re[2] = ['三码推荐',$a[0][2],$b[0][2],'上次推荐',$c[0][2],$d[0][2]];
        $re[3] = ['四码推荐',$a[0][3],$b[0][3],'上次推荐',$c[0][3],$d[0][3]];
        $re[4] = ['四码推荐',$a[0][4],$b[0][4],'上次推荐',$c[0][4],$d[0][4]];

        return response()->json(['msg'=>1,'data' => $re]);
    }
}

